// @ts-ignore
/* eslint-disable */
import { PageData, PageParams } from '@/common/page';
import { request } from '@umijs/max';

export async function getReportStressPage(
  data: PageParams,
  options?: { [key: string]: any },
) {
  return request<{
    code: number;
    msg?: string;
    data?: PageData;
  }>('/data-service/api/v1/report/page', {
    method: 'POST',
    data,
    ...(options || {}),
  });
}

export async function batchDelCase(
  data: {
    [key: string]: any
  },
  options?: { [key: string]: any },
) {
  return request<{
    code: number;
    msg?: string;
    data?: number
  }>('/data-service/api/v1/report/batch_del', {
    method: 'delete',
    data,
    ...(options || {}),
  });
}

export async function exportExcel(params: {
  [key: string]: any
}, options?: { [key: string]: any }) {
  return request<Blob>('/data-service/api/v1/report/export', {
    params,
    responseType: 'blob',
    getResponse: true,
    ...(options || {}),
  }).then(res => {
    // console.log(res)
    // console.log(res.headers['set-cookie']);
    const str = res.headers['content-disposition']
    if (!res || !str) {
      // error(res.message || '下载失败！')
      return
    }
    let fileName
    if (res?.headers['content-disposition']) {
      fileName = res?.headers['content-disposition'].replace(/\w+;filename=(.*)/, '$1')
    } else {
      fileName = params.fileName
    }
    
    // 此处当返回json文件时需要先对data进行JSON.stringify处理，其他类型文件不用做处理
    const dom = document.createElement('a')
    const downUrl = window.URL.createObjectURL(res?.data)
    dom.href = downUrl
    dom.download = decodeURIComponent(fileName)
    dom.style.display = 'none'
    document.body.appendChild(dom)
    dom.click()
    dom?.parentNode?.removeChild(dom)
    window.URL.revokeObjectURL(options?.url)

    return res
  })
}
